<script setup lang="ts">
import { ref } from 'vue'
import { getArticleCateService, deleteArticleCateService } from '@/apis/article/index'
import { Edit, Delete } from '@element-plus/icons-vue'
import ChnnelEdit from '@/views/article/components/ChnnelEdit.vue'
import { ElMessage, ElMessageBox } from 'element-plus'
const channelList = ref([])
const loading = ref(false)
const dolog = ref()
const getChanneList = async () => {
  //发请求之前开启loading效果
  loading.value = true
  const res = await getArticleCateService()
  channelList.value = res.data.data
  //发完请求之后把loading关闭
  loading.value = false
}
//编辑
getChanneList()
const onEditChannel = (row: any) => {
  dolog.value.open({ ...row })
}
//删除
const onDelChannel = async (row: any) => {
  await ElMessageBox.confirm('你确认要删除该分类?', '温馨提示', {
    type: 'warning',
    confirmButtonText: '确认',
    cancelButtonText: '取消'
  })
  await deleteArticleCateService(row.id)
  ElMessage.success('删除成功')
  getChanneList()
}
//添加分类的逻辑
const onAddchannel = () => {
  dolog.value.open({})
}
//接受子组件传过来的数据进行重新渲染数据
const onSuccess = () => {
  getChanneList()
}
</script>

<template>
  <page-container title="文章分类">
    <template #extra>
      <el-button @click="onAddchannel">添加分类</el-button>
    </template>
    <el-table v-loading="loading" :data="channelList" style="width: 100%">
      <el-table-column type="index" label="序号" width="100" />
      <el-table-column prop="cate_name" label="分类名称" />
      <el-table-column prop="cate_alias" label="分类别名" />
      <el-table-column label="操作" width="150">
        <!-- row表示channelList的每一项 $index表示channelList的索引  -->
        <template #default="{ row }">
          <el-button :icon="Edit" plain circle type="primary" @click="onEditChannel(row)"></el-button>
          <el-button :icon="Delete" plain circle type="danger" @click="onDelChannel(row)"></el-button>
        </template>
      </el-table-column>
      <!-- 没有数据的情况 -->
      <template #empty>
        <el-empty description="暂无数据"></el-empty>
      </template>
    </el-table>
    <!-- 点击编辑删除的弹层 -->
    <ChnnelEdit ref="dolog" @success="onSuccess"></ChnnelEdit>
  </page-container>
</template>
<style lang="scss" scoped></style>
